package com.example.demo.mapper;

import com.example.demo.bean.Order;
import com.example.demo.bean.view.OrderView;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

@Mapper
@Repository("OrderMapper")
public interface OrderMapper {
    @Insert("insert into orderTable(orderNum, buyerNum, sellerNum, timestamp, orderCondition, deleteFlag, goodsNum, numOfGoods) " +
            "value(#{orderNum}, #{buyerNum}, #{sellerNum}, #{timestamp}, #{orderCondition}, #{deleteFlag}, #{goodsNum}, #{numOfGoods})")
    public boolean insertOrder(String orderNum, String buyerNum, String sellerNum, Timestamp timestamp, int orderCondition, int deleteFlag, String goodsNum, int numOfGoods);

    @Delete("delete from orderTable where " +
            "orderNum = #{orderNum}")
    public boolean deleteOrderByAdmin(String orderNum);//通过物理删除删除订单项

    //修改订单的状态
    @Update("update orderTable " +
            "set orderCondition = #{orderCondition}, numOfGoods = #{numOfGoods} " +
            "where orderNum = #{orderNum}")
    public boolean updateOrder(String orderNum, int orderCondition, int numOfGoods);

    @Update("update orderTable " +
            "set deleteFlag = #{deleteFlag} " +
            "where orderNum = #{orderNum}")
    public boolean deleteOrderByUser(String orderNum, int deleteFlag);//通过修改标记的方式删除订单


    @Select("select * from orderTable")
    public List<Order> findAllOrder();

    //通过买家账户搜索订单
    @Select("SELECT orderTable.goodsNum, orderTable.sellerNum, orderTable.buyerNum, " +
            "orderTable.timestamp, orderTable.orderNum,  orderTable.orderCondition, " +
            "orderTable.deleteFlag, orderTable.numOfGoods, goods.goodsName, goods.imag, " +
            "goods.goodsIntroduction, goods.price, goods.goodsType, goods.goodsNum, " +
            "user.member " +//可添加用户其他属性
            "FROM User, orderTable, goods " +
            "WHERE orderTable.buyerNum = #{buyerNum} and  user.userNum = orderTable.buyerNum and goods.goodsNum = orderTable.goodsNum")
    public List<OrderView> findOrderByBuyerNum(String buyerNum);

    //搜索订单信息包括用户表，商品表，订单表
    @Select("SELECT orderTable.goodsNum, orderTable.sellerNum, orderTable.buyerNum, " +
            "orderTable.timestamp, orderTable.orderNum,  orderTable.orderCondition, " +
            "orderTable.deleteFlag, orderTable.numOfGoods, goods.goodsName, goods.imag, " +
            "goods.goodsIntroduction, goods.price, goods.goodsType, goods.goodsNum, " +
            "user.member " +//可添加用户其他属性
            "FROM User, orderTable, goods " +
            "WHERE user.userNum = orderTable.buyerNum and goods.goodsNum = orderTable.goodsNum")
    public List<OrderView> findAllOrderView();
    //通过卖家账户搜索订单
    @Select("SELECT orderTable.goodsNum, orderTable.sellerNum, orderTable.buyerNum, " +
            "orderTable.timestamp, orderTable.orderNum,  orderTable.orderCondition, " +
            "orderTable.deleteFlag, orderTable.numOfGoods, goods.goodsName, goods.imag, " +
            "goods.goodsIntroduction, goods.price, goods.goodsType, goods.goodsNum, " +
            "user.member " +//可添加用户其他属性
            "FROM User, orderTable, goods " +
            "WHERE orderTable.sellerNum = #{sellerNum} and user.userNum = orderTable.buyerNum and goods.goodsNum = orderTable.goodsNum")
    public List<OrderView> findOrderBySellerNum(String sellerNum);

    @Select("SELECT orderTable.goodsNum, orderTable.sellerNum, orderTable.buyerNum, " +
            "orderTable.timestamp, orderTable.orderNum,  orderTable.orderCondition, " +
            "orderTable.deleteFlag, orderTable.numOfGoods, goods.goodsName, goods.imag, " +
            "goods.goodsIntroduction, goods.price, goods.goodsType, goods.goodsNum, " +
            "user.member " +//可添加用户其他属性
            "FROM User, orderTable, goods " +
            "WHERE orderTable.orderNum = #{orderNum} and user.userNum = orderTable.buyerNum and goods.goodsNum = orderTable.goodsNum")
    public OrderView findOrderByOrderNum(String orderNum);
}
